package demo.projecttask.model;

import demo.base.StringUtils;
import demo.common.model.PagingCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llq on 2017/12/6.
 */
public class TaskCondition extends PagingCondition {

    private Boolean deleted = false;
    private Integer parentid = Task.ROOT;
    private String category;
    private String status;

    private String month;

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Integer getParentid() {
        return parentid;
    }

    public void setParentid(Integer parentid) {
        this.parentid = parentid;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public List<String> whereSQL() {
        List<String> sqls = new ArrayList<>();

        sqls.add(eq("deleted"));
        sqls.add(eq("parentid"));

        if(StringUtils.isNotEmpty(getSearch())){
            sqls.add(containBySearch("name", "principalName"));
        }

        if(StringUtils.isNotEmpty(getCategory())){
            sqls.add(eq("category"));
        }

        if("PROCESSING".equals(status)){
            sqls.add(in("status", new String[]{Task.Status.INIT.toString(), Task.Status.PROCESSING.toString()}));
        }else if("FINISHED".equals(status)){
            sqls.add(eq("status", Task.Status.FINISHED.toString()));
            sqls.add(" end_date >= real_end_date ");
        }else if("DELAYED".equals(status)){
            sqls.add(" end_date < real_end_date ");
        }

        if(StringUtils.isNotEmpty(month)){
            String[] ymStrings = month.split("-");
            int[] ym = {Integer.parseInt(ymStrings[0]), Integer.parseInt(ymStrings[1])};
            sqls.add("Year(create_date) = " + ym[0] + " and Month(create_date) = " + ym[1]);
        }

        return sqls;
    }

    @Override
    public String orderSQL() {
        return "order by id desc";
    }
}
